//
//  MyWindowController.m
//  XPComDemo
//
//  Created by cpdc on 12. 4. 28..
//  Copyright (c) 2012년 __MyCompanyName__. All rights reserved.
//

#import "MyWindowController.h"
#import "CustomView.h"
#include "xpcom-config.h"
//static MyWindowController *windowController = nil;

@implementation MyWindowController

@synthesize currentBundle;

- (id)init{
    
    NSBundle *curBundle = [NSBundle bundleForClass:[self class]];
    NSString *curPath = [curBundle bundlePath];
    curPath = [curPath stringByAppendingString:@"/mybundle.bundle"];
    NSBundle *bundle = [[NSBundle alloc] initWithPath:curPath];
    currentBundle = bundle;
    NSString* xibPath = [bundle pathForResource:@"MyWindow" ofType:@"nib"];
    NSLog(@"%@", xibPath);
    
    if (self=[super initWithWindowNibPath:xibPath owner:self]) {
        
    }
    return self;
    
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    //[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]
    NSString *title = [currentBundle localizedStringForKey:@"k_moisture_status_dry" value:@" " table:nil];
    [self.window setTitle:title];
}

- (void)windowWillClose:(NSNotification *)notification{
    // stop the modal runloop
    [NSApp stopModal];
}


//- (id)initWithWindow:(NSWindow *)window
//{
//    self = [super initWithWindow:window];
//    if (self) {
//        // Initialization code here.
//    }
//    
//    return self;
//}
//
//- (void)windowDidLoad
//{
//    [super windowDidLoad];
//    
//    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
//}

- (void)realShowDialog{
    
    NSBundle *curBundle = [NSBundle bundleForClass:[self class]];
    NSString *curPath = [curBundle bundlePath];
    curPath = [curPath stringByAppendingString:@"/mybundle.bundle"];
    NSBundle *bundle = [[NSBundle alloc] initWithPath:curPath];
    
    NSString* xibPath = [bundle pathForResource:@"MyWindow" ofType:@"nib"];
    
    MyWindowController *ctl = [[MyWindowController alloc] initWithWindowNibName:xibPath];
    
    [ctl showWindow:nil];
    return;
    
//    NSLog(@"start show dialog");
//    w = [[NSWindow alloc] init];
//    [w setStyleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask
//     | NSResizableWindowMask];
//    [w setFrame:NSMakeRect(0, 0, 300, 500) display:NO];
//    [w setReleasedWhenClosed:NO];
//    [w setLevel:NSPopUpMenuWindowLevel];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_windowClosed:) name:NSWindowWillCloseNotification object:w];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_windowShouldClose:) name:NSApplicationDidResignActiveNotification object:nil];
    

    
    NSString* filePath = [bundle pathForResource:@"large" ofType:@"png"];
    
    NSImageView *v = [[NSImageView alloc] init];
    [v setFrame:NSMakeRect(0, 0, 50, 50)];
    
    NSData *d = [NSData dataWithContentsOfFile:filePath];
    NSImage *img = [[NSImage alloc] initWithData:d];

    
    [v setImage:img];
//    CustomView *cv = [[CustomView alloc] initWithFrame:NSMakeRect(0, 0, 20, 20)];
    
    [[w contentView] addSubview:v];
    [v release];
//    [cv release];
    
    //NSLog(@"start");    
    [w makeKeyAndOrderFront:nil];
    //[NSApp runModalForWindow:w];
    
    _eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask | NSKeyDownMask handler:^(NSEvent *incomingEvent) {
        NSEvent *result = incomingEvent;
        NSWindow *targetWindowForEvent = [incomingEvent window];
        if (targetWindowForEvent != w) {
            [w close];
        } else if ([incomingEvent type] == NSKeyDown) {
            if ([incomingEvent keyCode] == 53) {
                // Escape
                [w close];
                result = nil; // Don't process the event
            } else if ([incomingEvent keyCode] == 36) {
                // Enter
                [w close];
                result = nil;
            }
        }
        return result;
    }];
    
    //[w release];
    //NSLog(@"stop");
    
    
    

    //    
    //    [windowController showWindow:nil];
    //    [windowController.window setFrameOrigin:NSMakePoint(100, 100)];
}

- (IBAction)showAlert1:(id)sender{
    NSAlert *alert = [NSAlert alertWithMessageText:@"dsa" defaultButton:@"ccc" alternateButton:@"bbb" otherButton:nil informativeTextWithFormat:@"fdsafda"];
    [alert runModal];
}

- (void)showMyDialog{
    
    [self realShowDialog];
//    NSLog(@" performSelector start");
//    //[self performSelector:@selector(realShowDialog) withObject:nil];
////    [self performSelectorOnMainThread:@selector(realShowDialog) withObject:nil waitUntilDone:NO];
//    NSLog(@" performSelector end");
    

}



// - (void)_windowShouldClose:(NSNotification *)note {
//     [w close];
// }
//                     
//- (void)_windowClosed:(NSNotification *)note {
// if (_eventMonitor) {
//     [NSEvent removeMonitor:_eventMonitor];
//     _eventMonitor = nil;
// }
//    
// [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillCloseNotification object:w];
// [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationDidResignActiveNotification object:nil];
//[w autorelease];
//w = nil;
//}



@end
